<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php



abstract class LoggerAppender {
	
	
	protected $closed = false;
	
	
	protected $filter = null;
			
	
	protected $layout = null; 
	
	
	protected $name;
	
	
	protected $threshold = null;
	
	
	protected $requiresLayout = true;
	
	
	public function __construct($name = '') {
		$this->name = $name;
	}

	
	public function addFilter($newFilter) {
		if($this->filter === null) {
			$this->filter = $newFilter;
		} else {
			$this->filter->addNext($newFilter);
		}
	}
	
	
	public function clearFilters() {
		unset($this->filter);
		$this->filter = null;
	}

	
	public function getFilter() {
		return $this->filter;
	} 
	
	
	public function getFirstFilter() {
		return $this->filter;
	}
	
	
	
	public function doAppend(LoggerLoggingEvent $event) {
		if($this->closed) {
			return;
		}
		
		if(!$this->isAsSevereAsThreshold($event->getLevel())) {
			return;
		}

		$f = $this->getFirstFilter();
		while($f !== null) {
			switch ($f->decide($event)) {
				case LoggerFilter::DENY: return;
				case LoggerFilter::ACCEPT: return $this->append($event);
				case LoggerFilter::NEUTRAL: $f = $f->getNext();
			}
		}
		$this->append($event);
	}	 

	
	public function setLayout($layout) {
		if($this->requiresLayout()) {
			$this->layout = $layout;
		}
	} 
	
	
	public function getLayout() {
		return $this->layout;
	}
	
	
	public function requiresLayout() {
		return $this->requiresLayout;
	}
	
	
	public function getName() {
		return $this->name;
	}
	

	public function setName($name) {
		$this->name = $name;	
	}
	
	
	public function getThreshold() { 
		return $this->threshold;
	}
	
	
	public function setThreshold($threshold) {
		if(is_string($threshold)) {
			$this->threshold = LoggerOptionConverter::toLevel($threshold, null);
		} else if($threshold instanceof LoggerLevel) {
			$this->threshold = $threshold;
		}
	}
	
	
	public function isAsSevereAsThreshold($level) {
		if($this->threshold === null) {
			return true;
		}
		return $level->isGreaterOrEqual($this->getThreshold());
	}

	
	abstract public function activateOptions();
	
	
	abstract protected function append(LoggerLoggingEvent $event); 

	
	abstract public function close();
}
